package com.diviner.jsontransform.controller;

import com.diviner.jsontransform.service.JsonToExcelService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@Api(value = "Json转为Excel", tags = {"Json转为Excel"})
public class JsonToExcelController {

    @Autowired
    private JsonToExcelService jsonToExcelService;

    /**
     * 上传json字符在传为Excel
     * @param json
     * @param response
     * @throws IOException
     */
    @PostMapping("/toExcel")
    @ApiOperation(value = "Swagger JSON数据转换为 excel",notes = "传入字符串")
    public void convertJsonToExcel(@ApiParam(value = "Swagger JSON字符串", required = true) @RequestBody String json, HttpServletResponse response) throws IOException {
        jsonToExcelService.jsonToExcel(json,response);
    }

    /**
     * 上传txt文件在传为Excel
     * @param file
     * @param response
     * @throws IOException
     */
    @PostMapping("/uploadJsonToExcel")
    @ApiOperation(value = "上传一个txt转换为 excel",notes = "传入txt")
    public void uploadJsonToExcel(@ApiParam(value = "Swagger JSON字符串文本", required = true) @RequestParam("file") MultipartFile file, HttpServletResponse response) throws IOException {
        jsonToExcelService.uploadJsonToExcel(file,response);
    }
}